package gui;

import javax.swing.JPanel;
import javax.swing.JApplet;
import javax.swing.JScrollPane;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Dimension;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.LinkedList;
import javax.swing.JScrollBar;
import javax.swing.JButton;
import server.TweetStack;
import client.ClientInterface;
import common.Tweet;
import javax.swing.JTextPane;

public class MyGui extends JApplet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null; // @jve:decl-index=0:visual-constraint="10,30"
	private JLabel AppletTitle = null;
	private JPanel FrontPanel = null;
	private JLabel jLabel = null;
	private JLabel HashTagLabel = null;
	private String hashTag = "#"; // @jve:decl-index=0:
	private String skinColor = "blue"; // @jve:decl-index=0:
	private JScrollBar jScrollBar = null;
	protected Graphics g;
	private JButton jColorButton = null;
	private JLabel HashTagLabel2 = null;
	private JLabel HashTagLabel3 = null;
	private String labelText; // @jve:decl-index=0:
	private JScrollBar jScrollBar1 = null;
	private JScrollPane jScrollPane = null;

	/**
	 * This is the xxx default constructor
	 */
	public MyGui() {
		super();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	public void init() {

		final Runnable a = new Runnable() {
			public void run() {
				guiInit(); // initialize the GUI
				start();
			}
		};
		final Thread appThread = new Thread() {
			public void run() {
				try {
					System.out.println("Setting up ");
					Registry registry = LocateRegistry.getRegistry(getParameter("ip"));
					ClientInterface stub = (ClientInterface) registry.lookup(getParameter("interface"));
					int tweetCounter = 0;
					String textBuffer = "";
					Tweet lastTweet = new Tweet();
					lastTweet.setId(0);
					while (true) {
						if(tweetCounter>20) {
							labelText = textBuffer;
							tweetCounter=0;
						}
						TweetStack tweets = stub.getTweets(lastTweet); 
						System.out.println("Getting tweets ");
						if (tweets != null) {
							LinkedList<Tweet> stack = tweets.getStack();
							int size = stack.size();
							textBuffer = "";  
							tweetCounter = tweetCounter + size;
							for (int i = 0; i < size; i++) 
							{
							System.out.println("Printing Tweets");
							lastTweet = tweets.pop();
							System.out.println(lastTweet.toString());
							textBuffer = lastTweet.toString() + textBuffer; 
							}
							labelText = textBuffer + labelText; 
							String head = "<html><div style='overflow:auto; height:100px; width:300px'><table cellpadding='0' cellspacing='0' style='height:250px;'><tr><td>";
							String tail = "</td></tr></table></div></html>"; 
							jLabel.setText(head+labelText+tail);
							jLabel.validate();
						}
						System.out.println("Sleeping");
						Thread.sleep(300);

					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};

		a.run();

		appThread.start();

	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {

			HashTagLabel3 = new JLabel();
			HashTagLabel3.setBounds(new Rectangle(287, 35, 103, 21));
			HashTagLabel3.setForeground(Color.white);
			HashTagLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
			HashTagLabel3.setHorizontalTextPosition(SwingConstants.RIGHT);
			HashTagLabel3.setText(getParameter("hash3"));
			HashTagLabel3.setFont(new Font("Gill Sans MT Condensed", Font.BOLD,
					14));
			HashTagLabel2 = new JLabel();
			HashTagLabel2.setBounds(new Rectangle(287, 19, 103, 21));
			HashTagLabel2.setForeground(Color.white);
			HashTagLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
			HashTagLabel2.setHorizontalTextPosition(SwingConstants.RIGHT);
			HashTagLabel2.setText(getParameter("hash2"));
			HashTagLabel2.setFont(new Font("Gill Sans MT Condensed", Font.BOLD,
					14));
			HashTagLabel = new JLabel();
			HashTagLabel.setBounds(new Rectangle(287, 3, 103, 21));
			HashTagLabel.setForeground(Color.white);
			HashTagLabel.setText(getParameter("hash"));
			HashTagLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			HashTagLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
			HashTagLabel.setFont(new Font("Gill Sans MT Condensed", Font.BOLD,
					14));
			AppletTitle = new JLabel();
			AppletTitle.setBounds(new Rectangle(14, 15, 140, 27));
			AppletTitle.setFont(new Font("Kristen ITC", Font.BOLD, 19));
			AppletTitle.setForeground(Color.white);
			AppletTitle.setText("TweetMeToo");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			if (getParameter("col") != null) {
				skinColor = getParameter("col").toString();
			}
			System.out.println(skinColor);
			System.out.println(skinColor.equals("red"));
			if (skinColor.equals("blue")) {
				jContentPane.setBackground(new Color(0, 102, 153));
				jContentPane.setSize(new Dimension(415, 629));
			} else if (skinColor.equals("red")) {
				jContentPane.setBackground(new Color(150, 0, 0));
			} else if (skinColor.equals("black")) {
				jContentPane.setBackground(new Color(0, 0, 0));
			} else if (skinColor.equals("green")) {
				jContentPane.setBackground(new Color(0, 150, 0));
			} else {
				jContentPane.setBackground(new Color(0, 150, 0));
			}

			
			jContentPane.add(AppletTitle, null);
			jContentPane.add(getFrontPanel(), null);
			jContentPane.add(HashTagLabel, null);
			jContentPane.add(HashTagLabel2, null);
			jContentPane.add(HashTagLabel3, null);
			jContentPane.add(getJColorButton(), null);
			
			
		}
		return jContentPane;
	}

	/**
	 * This method initializes FrontPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getFrontPanel() {

		if (FrontPanel == null) {
			jLabel = new JLabel();

			labelText = "";

			jLabel.setText(labelText);
			jLabel.setVerticalAlignment(SwingConstants.TOP);
			jLabel.setBounds(new Rectangle(0, 1, 399, 546));

			jLabel.setFont(new Font("Arial", Font.BOLD, 11));
			FrontPanel = new JPanel();
			FrontPanel.setLayout(null);
			FrontPanel.setBounds(new Rectangle(8, 57, 398, 545));
			FrontPanel.setBackground(Color.white);
			FrontPanel.add(jLabel, null);
			FrontPanel.add(getJScrollPane(), null);
			
			
		}
		return FrontPanel;
	}

	public void guiInit() {
		this.setSize(410, 630);
		this.setContentPane(getJContentPane());
		System.out.println("initiated");

	}

	public void start() {
		System.out.println("started");
	}

	// Called when the applet is stopped.
	public void stop() {
		System.out.println("stopped");
	}

	// Called when applet is terminated. This is
	// the last method executed.
	public void destroy() {
		System.out.println("destroyed");
	}

	/**
	 * This method initializes jColorButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJColorButton() {
		if (jColorButton == null) {
			jColorButton = new JButton();
			jColorButton.setBounds(new Rectangle(280, 606, 112, 19));
			jColorButton.setText("Change Color");
			jColorButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					System.out.println("mouseClicked()"); // TODO Auto-generated
															// Event stub
															// mouseClicked()
					if (skinColor.equals("black")) {
						jContentPane.setBackground(new Color(0, 102, 153));
						skinColor = "blue";
					} else if (skinColor.equals("blue")) {
						jContentPane.setBackground(new Color(150, 0, 0));
						skinColor = "red";
					} else if (skinColor.equals("green")) {
						jContentPane.setBackground(new Color(0, 0, 0));
						skinColor = "black";
					} else if (skinColor.equals("red")) {
						jContentPane.setBackground(new Color(0, 150, 0));
						skinColor = "green";
					}
				}

			});
		}
		return jColorButton;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane(jLabel);
			jScrollPane.setBounds(new Rectangle(-1, -2, 402, 549));
			//jScrollPane.getViewport().setBackground(Color.white);
			
		}
		return jScrollPane;
	}

}
